package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.DetailsofPaymentActivity;
import com.huifaapp.mall.activity.ReceiveAddressActivity;
import com.huifaapp.mall.bean.ReceiveBean;
import com.huifaapp.mall.uitils.Contig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac on 2018/1/3.
 */

public class ReceiveAddressAdapter extends RecyclerView.Adapter<ReceiveAddressAdapter.MyViewHolder> {
    private onAdapterItemClickListener listener;
    private Context context;
    private List<ReceiveBean.DataBean> lists;




    public void addorder(List<ReceiveBean.DataBean> lists){
        this.lists = lists;

    }

    public ReceiveAddressAdapter(Context context, List<ReceiveBean.DataBean> lists) {
        this.context = context;
        this.lists = lists;


    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.receiveaddress_item,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final ReceiveBean.DataBean dataBean = lists.get(position);
        //设置显示是否默认地址
        lists.get(position).setSelect(lists.get(position).getIsDefaultAddress().equals("1"));
        //holder.bt_check.setChecked(lists.get(position).isSelect());
        //holder.bt_check.setImageResource(R.drawable.porfile_icon_set_sel);
        if (lists.get(position).getIsDefaultAddress().equals("0")){
            holder.bt_check.setImageResource(R.drawable.porfile_icon_set_nor);
        }else{
            holder.bt_check.setImageResource(R.drawable.porfile_icon_set_sel);
        }


        Log.e("FHHH",lists.get(position).isSelect()+"");
        holder.bt_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v, position);
                }
            }
        });

        holder.rece_name.setText(dataBean.getReceiverName());
        holder.rece_phone.setText(dataBean.getReceiverPhone()+"");
        holder.rece_address.setText(dataBean.getCity()+" 详细地址:"+dataBean.getDetail());


        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener!=null){
                    onItemClickListener.onDelete(position);
                }
            }
        });
        holder.bianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener!=null){
                    onItemClickListener.onBianji(position);
                }
            }
        });


        holder.ll_dianjifanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if (onItemClickListener!=null){
                   onItemClickListener.onIntentadree(position);
               }

            }
        });

    }



    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView bt_check;
        TextView rece_name,rece_phone,rece_address;
        LinearLayout  delete;
        LinearLayout  bianji;
        LinearLayout ll_dianjifanhui;


        public MyViewHolder(View itemView) {
            super(itemView);
            bt_check = (ImageView) itemView.findViewById(R.id.receiveaddress_check);
            rece_name = (TextView) itemView.findViewById(R.id.receive_recename);
            rece_phone = (TextView) itemView.findViewById(R.id.receive_recephone);
            rece_address = (TextView) itemView.findViewById(R.id.rece_address);
            delete = (LinearLayout) itemView.findViewById(R.id.receiveaddress_delete);
            bianji = (LinearLayout) itemView.findViewById(R.id.receiveaddress_bianji);
            ll_dianjifanhui= (LinearLayout) itemView.findViewById(R.id.ll_dianjifanhui);
        }
    }

    /*
    * 监听器接口
    * */
    public interface onAdapterItemClickListener {
        void onClick(View view, int position);

    }

    private onAdapterItemClickListener onitem;

    public void setAdapterClickListener(onAdapterItemClickListener listener) {
        this.listener = listener;
    }

    public List<ReceiveBean.DataBean> getAdapterData() {
        return lists;
    }






    public interface OnItemClickListener {
        void onDelete(int position);
        void onBianji(int position);
        void onIntentadree(int position);

    }
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}
